<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/22
 * Time: 9:14
 */

namespace app\admin\controller;

use think\Controller;
use app\admin\model\Job;
use think\Request;
use think\File;

class Excelud extends Controller
{
    //excel导入
    public function upload_excel(){
        //设置文件上传的最大限制
        ini_set('memory_limit','1024M');
        //加载第三方类文件
//        var_dump(vendor('PHPExcel'));
        vendor('PHPExcel');
//        var_dump(vendor('PHPExcel.Classes.PHPExcel_IOFactory'));exit;
        //防止乱码
        header("Content-type:text/html;charset=utf-8");
        //实例化主文件
//        $model = new \PHPExcel();
        //接收前台传过来的execl文件
        $file = $_FILES['excel'];
        //截取文件的后缀名，转化成小写
        $extension = strtolower(pathinfo($file['name'],PATHINFO_EXTENSION));
        if($extension == "xlsx"){
            //2007(相当于是打开接收的这个excel)
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        }else{
            //2003(相当于是打开接收的这个excel)
            $objReader =  \PHPExcel_IOFactory::createReader('Excel5');
        }

        $objContent = $objReader -> load($file['tmp_name']);
        $sheetContent = $objContent -> getSheet(0) -> toArray();
        unset($sheetContent[0]);
        $res=[];
        foreach ($sheetContent as $k => $v){
            $arr['job_name'] = $v[0];
            $arr['job_sex'] = $v[1];
            $arr['job_work'] = $v[2];
            $arr['job_days'] = $v[3];
            $arr['job_year'] = $v[4];
            $arr['job_phone'] = $v[5];
            $arr['job_email'] = $v[6];
            $arr['job_onaddress'] = $v[7];
            $arr['job_postal_address'] = $v[8];
            $arr['job_host'] = $v[9];
            $arr['job_unit'] = $v[10];
            $arr['job_school'] = $v[11];
            $arr['job_education'] = $v[12];
            $arr['job_major'] = $v[13];
            $arr['job_source'] = $v[14];
            $arr['job_createtime'] = time();
            if(!in_array($arr,$res)){
                $res[] = $arr;
            }
        }
        $res = $this->array_unset_tt($res,'job_sex');
        $jobs = [];
        $info = Job::all();
//        对象转数组
        foreach($info as $v){
            $jobs[] = $v->toArray();
        }
       foreach ($res as $k=>$v){
            foreach ($jobs as $q){
                if($v['job_sex']==$q['job_phone']){
                    unset($res[$k]);
                }
            }
       }
//        var_dump($res);exit;
        $res = Job::insertAll($res);
        if($res){
            echo "<script>alert('导入信息成功！');location.href='".$_SERVER["HTTP_REFERER"]."';</script>"; // 返回上一页并刷新
        }else{
            echo "<script>alert('导入信息失败！');location.href='".$_SERVER["HTTP_REFERER"]."';</script>"; // 返回上一页并刷新
        }
    }
    /**
     * 导入数据与数据库数据比对消除重复数据
     * @param $arr1
     * @param $arr2
     * @param $key
     */
    public function array_unset_bb($arr1,$arr2,$key){
        foreach ($arr1 as $k=>$v){
            foreach ($arr2 as $q){
                if($v[$key]==$q[$key]){
                    unset($arr1[$k]);
                }
            }
        }
    }
    /**
     * 二维数组去重
     * @param $arr
     * @param $key
     * @return array
     */
    public  function array_unset_tt($arr, $key)
    {
        //建立一个目标数组
        $res = array();
        foreach ($arr as $value) {
            //查看有没有重复项
            if (isset($res[$value[$key]])) {
                //有：销毁
                unset($value[$key]);
            } else {
                $res[$value[$key]] = $value;
            }
        }
        return $res;
    }
//    简历上传
    public function upload_word(Request $req){
        $job_id = $req->param('job_id');
        $file = $req->file('word');      //获取表单提交过来的文件
        $error = $_FILES['word']['error'];
        if($error>0){
            echo "<script>alert('文件上传失败！');location.href='".$_SERVER["HTTP_REFERER"]."';</script>"; // 返回上一页并刷新
        }
        //获取上传文件的原文件名
//        $filename = $file->getInfo()['name'];
        $dir = config('word_upload_path');  //自定义word文件上传路径
        if(!is_dir($dir)){
            mkdir($dir,0777,true);
        }
        $info = $file->move($dir);  //将文件上传指定目录
        // 获取文件的全路径
        $word_url = str_replace('\\','/',$info->getSaveName());
//        echo $job_id.'--'.$word_url;exit;
        $word_name = explode('.',$info->getFilename())[0].'.pdf';
        $this->word_preview($word_url,$word_name);
        $res = Job::where('job_id',$job_id)
        ->update([
            'job_resume'=>$word_url,
            'job_resume_pdf'=>$word_name
        ]);
        if($res>=0){
            echo "<script>alert('简历上传成功！');location.href='".$_SERVER["HTTP_REFERER"]."';</script>"; // 返回上一页并刷新
        }else{
            unlink($word_url);//删除刚上传的文件
            echo "<script>alert('简历上传失败！');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
        }
    }
//    word 转 pdf
    public function word_preview($word_url,$word_name){
        include 'preview.php';
        header("content-type:text/html;charset=utf-8");
//        dump($req->param('word_name'));
        $wordname = config('word_upload_path').$word_url;
        $wordname = str_replace('\\','/',$wordname);
//        $wordname = 'file:///'.str_replace('\'','',$wordname);
        $wordname = str_replace('\'','',$wordname);
//        echo $wordname;exit;
        $pdfname = config('pdf_upload_path').$word_name;
//        $pdfname = 'file:///'.str_replace('\\','/',$pdfname);
        $pdfname = str_replace('\\','/',$pdfname);
//        include $down;
//        php_Word($wordname,"word_preview.html");
        word2pdf($wordname,$pdfname);
//        $namepdf=explode("/",$pdfname);
//      $_SESSION["pdfname"]=$pdfname;
//      [count($namepdf)-1];
//        $url = config('word_upload_path').'/pdfdemo.html';
//        include $url;
//        word2pdf('file:///C:/Users/lenovo/Documents/shenqingshu.doc','file:///C:/Users/lenovo/Documents/11.pdf');
//        require  $url;

    }
//    简历预览
    public function pdf_preview(Request $req){
        $job_id = $req->param('pdf_name');
        $job_id = str_replace('\'','',$job_id);
        $res = Job::where('job_id',$job_id)->field('job_resume,job_resume_pdf')->find();
        $pdf_name = $res['job_resume_pdf'];
        $word_name = $res['job_resume'];
        $down = config('word_upload_path').'download.html';
//        $this->assign('pdf_name',$pdf_name);
//        dump($_SERVER['DOCUMENT_ROOT']);exit;
       
        require $down;
        require ROOT_PATH.'/application/admin/view/phone/pdfdemo.html';
//        $this->fetch('phone/pdfdemo');
    }
//    简历下载
    public function download(Request $req){
        $word_name = $req->param('word_name');
        $word_name_path = config('word_upload_path').$word_name;
        header( "Content-Disposition:  attachment;  filename=".$word_name); //告诉浏览器通过附件形式来处理文件
        header('Content-Length: ' . filesize($word_name_path)); //下载文件大小
        readfile($word_name_path);  //读取文件内容
    }
}